/**
 * 21/02/2014
 * 
 * SaleController
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.SaleBOInterface;
import org.cuatrovientos.erp.model.Sale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controller class for Sale management
 * @author Javier Tejedor
 */
@Controller
public class SaleController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(SaleController.class);

	@Autowired
	private SaleBOInterface saleBO;

	/**
	 * default constructor
	 */
	public SaleController () {
		logger.info("Sale BEAN instantiated");
	}

	/**
	 * handles default /sale or /sale/list request
	 * @return
	 */
	@RequestMapping({"/sale","/sale/list"})
	public ModelAndView saleDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Sale> saleList = saleBO.selectAll();
		model.put("sales", saleList );
		
		return new ModelAndView("SaleList", model);     
	}


	/**
	 * handles default /sale/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/sale/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true) 
			String description) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Sale> saleList = saleBO.search(description);
		model.put("sales", saleList);
		
		return new ModelAndView("SaleList", model);     
	}

	/**
	 * handles default /sale/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/sale/delete"})
	public String deleteSale(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("delete " + id);

		saleBO.deleteById(id);
		
		return "redirect:/sale";
	}

	/**
	 * handles default /sale/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/sale/new"})
	public ModelAndView newSale() {
		logger.info("new sale");
		
		Sale sale = new Sale();

		return new ModelAndView("NewSale", "sale", sale);
	}

	/**
	 * handles default /sale/save request
	 * @param sale
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/sale/save"})
	public String newSale(@Valid Sale sale, BindingResult bindingResult) {
		logger.info("new sale " + sale.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("sale", sale);
			
			return "NewSale";
		}

		saleBO.insert(sale);
		
		return "redirect:/sale";
	}

	/**
	 * handles default /sale/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/sale/detail"})
	public ModelAndView saleDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Sale sale = saleBO.selectById(id);
		model.put("sale", sale);

		return new ModelAndView("SaleDetail", model);
	}


	/**
	 * handles default /sale/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/sale/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Sale sale = saleBO.selectById(id);
		model.put("sale", sale);
		
		return new ModelAndView("UpdateSale",model);
	}


	/**
	 * handles default /sale/saveupdate request
	 * @param sale
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/sale/saveupdate"})
	public String saveUpdate(@Valid Sale sale, BindingResult bindingResult) {
		logger.info("update sale " + sale.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("sale", sale);
			
			return "UpdateSale";
		}

		saleBO.update(sale);
		
		return "redirect:/sale";
	}
	
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping({"/sale/index"})
	public String sayHelloPage(Map<String, Object> model) {
		return "index/IndexSale";
	}
}
